package pl.mchaniewski.ryneczek.utilities;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateUtil {
    private static final Logger logger = LoggerFactory
            .getLogger(DateUtil.class);
    public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String SIMPLE_DATE_FORMAT = "dd-MM-yyyy";
    public static final String TIME_FORMAT = "HH:mm";

    public static String getCurrentTime() {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);

        return sdf.format(new Date());
    }

    public static String dateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);

        return sdf.format(date);
    }

    public static String simpleDateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(SIMPLE_DATE_FORMAT);

        return sdf.format(date);
    }

    public static String timeToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT);

        return sdf.format(date);
    }

    public static Date setTime(Date date, Integer hour, Integer minute,
            Integer second) {
        Calendar tempDate = Calendar.getInstance();
        tempDate.setTime(date);
        tempDate.set(Calendar.HOUR_OF_DAY, hour);
        tempDate.set(Calendar.MINUTE, minute);
        tempDate.set(Calendar.SECOND, second);

        return tempDate.getTime();
    }

    public static Date setDate(Date date, int day, int month, int year) {
        Calendar tempDate = Calendar.getInstance();
        tempDate.setTime(date);
        tempDate.set(Calendar.DAY_OF_MONTH, day);
        tempDate.set(Calendar.MONTH, month);
        tempDate.set(Calendar.YEAR, year);

        return tempDate.getTime();
    }

    public static Date getNow() {
        return new Date();
    }

    public static boolean isDateBeforeNow(Date date) {
        Calendar tempDate = Calendar.getInstance();
        tempDate.setTime(date);

        Calendar tempDateNow = Calendar.getInstance();
        tempDateNow.setTime(getNow());

        if (tempDate.get(Calendar.YEAR) > tempDateNow.get(Calendar.YEAR)) {
            return false;
        }

        if (tempDate.get(Calendar.YEAR) < tempDateNow.get(Calendar.YEAR)) {
            return true;
        }

        if (tempDate.get(Calendar.MONTH) > tempDateNow.get(Calendar.MONTH)) {
            return false;
        }

        if (tempDate.get(Calendar.MONTH) < tempDateNow.get(Calendar.MONTH)) {
            return true;
        }

        if (tempDate.get(Calendar.DAY_OF_MONTH) > tempDateNow
                .get(Calendar.DAY_OF_MONTH)) {
            return false;
        }

        if (tempDate.get(Calendar.DAY_OF_MONTH) < tempDateNow
                .get(Calendar.DAY_OF_MONTH)) {
            return true;
        }

        return false;
    }

    public static boolean isDateAfterWhen(Date date, Date when) {
        return date.after(when);
    }

    public static boolean isHolidayToday(String date, String dateFormat) {
        Calendar now = Calendar.getInstance();
        now.setTime(getNow());

        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        Date holiday = null;

        try {
            holiday = sdf.parse(date);
        } catch (ParseException ex) {
            logger.warn(
                    "Problems with parsing date: {} in format: {}. Fix DateHelper or SchedulerService.",
                    date, dateFormat);
            return false;
        }

        Calendar holidayCalendar = Calendar.getInstance();
        holidayCalendar.setTime(holiday);

        if (now.get(Calendar.MONTH) == holidayCalendar.get(Calendar.MONTH)
                && now.get(Calendar.DAY_OF_MONTH) == holidayCalendar
                        .get(Calendar.DAY_OF_MONTH)) {
            return true;
        }

        return false;
    }

    public static Date addTime(Date date, Integer hour, Integer minute,
            Integer second) {
        Calendar dateTemp = Calendar.getInstance();
        dateTemp.setTime(date);

        dateTemp.add(Calendar.HOUR_OF_DAY, hour);
        dateTemp.add(Calendar.MINUTE, minute);
        dateTemp.add(Calendar.SECOND, second);

        return dateTemp.getTime();
    }

    public static Date addDate(Date date, Integer day, Integer month,
            Integer year) {
        Calendar dateTemp = Calendar.getInstance();
        dateTemp.setTime(date);

        dateTemp.add(Calendar.DAY_OF_MONTH, day);
        dateTemp.add(Calendar.MONTH, month);
        dateTemp.add(Calendar.YEAR, year);

        return dateTemp.getTime();
    }
}
